<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){	
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$fields=array('id','title','logo','status','group_id','ccate','scate');
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_cmso2o_list',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$item){
		$item['link']=custom_url('jy_cmso2o/detail//id='.$item['id'],true);
		$item['pcatetitle']=pdo_getcolumn('jy_cmso2o_group',array('id'=>$item['group_id']),'title');
		$item['pcatetitle']=$item['pcatetitle']?:'无';
		$item['ccatetitle']=pdo_getcolumn('jy_cmso2o_group',array('id'=>$item['ccate']),'title');
		$item['ccatetitle']=$item['ccatetitle']?:'无';
		$item['scatetitle']=pdo_getcolumn('jy_cmso2o_group',array('id'=>$item['scate']),'title');
		$item['scatetitle']=$item['scatetitle']?:'无';
	}
	$return['status']=array('0'=>'待审核','1'=>'已审核','2'=>'已驳回');
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'商户管理'),
			array('op'=>'shenhe','title'=>'待审核商户'),
			array('op'=>'edit','title'=>'添加商户'),
		),
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：这里是商户黄页信息列表，如果该商户自己添加了小店(<red>有些商户是不需要小店的，只是展示自己公司的黄页信息而已</red>)，前端就会显示小店入口！','list'=>array(
			array('name'=>'title','title'=>'名称','type'=>'string'),
			array('type'=>'img_string','title'=>'分类',
			'data'=>array(
				array('title'=>'一级：_Level 1：','name'=>'pcatetitle'),
				array('title'=>'二级：_Level 2：','name'=>'ccatetitle'),
				array('title'=>'三级：_Level 3：','name'=>'scatetitle')
				)
			),
			array('name'=>'logo','title'=>'图片','type'=>'image'),
			array('name'=>'link','title'=>'商户地址','type'=>'copy'),				
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
		),
		),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'名称','type'=>'string'),
			array('name'=>'status','width'=>15,'title'=>'状态','type'=>'select','options'=>'status2'),
		)
	);
	$return['htmls']=$htmls;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	exi($return);
}
if($_RQ['op']=='shenhe'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>0);
	$fields=array('id','title','logo','status');
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_cmso2o_list',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$return['status']=array('0'=>'待审核','1'=>'已审核','2'=>'已驳回');
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'商户管理'),
			array('op'=>'shenhe','title'=>'待审核商户'),
			array('op'=>'edit','title'=>'添加商户'),
		),
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'title','title'=>'名称','type'=>'string'),
			array('name'=>'logo','title'=>'图片','type'=>'image'),								
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
		),
		'action'=>array(	
			array('type'=>'href','op'=>'yes','title'=>'通过'),
			array('type'=>'href','op'=>'no','title'=>'驳回'),
		),
		),
		
	);
	$return['htmls']=$htmls;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	exi($return);
}
if($_RQ['op']=='yes'){
		$id=intval($_RQ['id']);
		if($id){
			pdo_update('jy_cmso2o_list',array('status'=>1),array('id'=>$id));
			exi('已通过','','/list/');
		}else{
			exi('请先选择商户','error');
		}
}
if($_RQ['op']=='no'){
		$id=intval($_RQ['id']);
		if($id){
			pdo_update('jy_cmso2o_list',array('status'=>2),array('id'=>$id));
			exi('已驳回','','/list/');
		}else{
			exi('请先选择商户','error');
		}
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_cmso2o_list',array('id'=>$id));
		$item['lat_lng']=json_decode($item['lat_lng'],true);
		$item['hdp']=json_decode($item['hdp'],true);
		$item['onlinetime']=json_decode($item['onlinetime'],true);
		$item['location']=array('province'=>$item['province'],'city'=>$item['city'],'district'=>$item['district']);
		//print_r($item);exit();
	}else{
		$item=array();
	}
    $return['item']=$item;
	$return['group']=pdo_getall('jy_cmso2o_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['ishow']=array('0'=>'不显示','1'=>'显示');
	$return['status']=array('0'=>'待审核','1'=>'已审核','2'=>'已驳回');
	$return['is']=array('0'=>'否','1'=>'是');
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'商户管理'),
			array('op'=>'shenhe','title'=>'待审核商户'),
			array('op'=>'edit','title'=>'添加商户')
		),
		'edit'=>array(
			array('name'=>'title','title'=>'名称','type'=>'string'),
			array('name'=>'logo','title'=>'logo','type'=>'image'),
			array('name'=>'uid','title'=>'选择管理员','type'=>'choose_title','action'=>'get_member'),	
			array('name'=>'onlinetime','title'=>'营业时间','type'=>'time_hour'),
			array('name'=>'lat_lng','title'=>'坐标','type'=>'lat_lng'),
			array('name'=>'address','title'=>'地址','type'=>'string'),
			array('type'=>'select3_line','name'=>'group_id','options'=>'pcates','title'=>'选择分类',
				'name2'=>'ccate','options2'=>'ccates','title2'=>'请选择二级分类',
				'name3'=>'scate','options3'=>'scates','title3'=>'请选择三级分类',
			),
			array('name'=>'location','title'=>'地区','type'=>'pcd'),
			array('name'=>'price','title'=>'价格','type'=>'string'),
			array('name'=>'username','title'=>'联系人','type'=>'string'),
			array('name'=>'phone','title'=>'联系电话','type'=>'string'),
			array('name'=>'desc','title'=>'简介','type'=>'text'),
			array('name'=>'hdp','title'=>'幻灯片','type'=>'images'),
			//array('name'=>'ewm','title'=>'二维码','type'=>'image'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'ishow','title'=>'是否显示','type'=>'radio','options'=>'ishow'),
			array('name'=>'ishome','title'=>'是否首页显示','type'=>'radio','options'=>'is'),
			array('name'=>'ishot','title'=>'是否热门','type'=>'radio','options'=>'is'),
			)
	);
	$groupspram=array('uniacid'=>$_SESSION['uniacid'],'parentid'=>0);
	$return['pcates']=pdo_getall('jy_cmso2o_group',$groupspram,array('title','id'),'id');
	foreach($return['pcates'] as &$pv){
		$pv['ccates']=pdo_getall('jy_cmso2o_group',array('uniacid'=>$_SESSION['uniacid'],'parentid'=>$pv['id']),array('title','id'),'id');
		foreach($pv['ccates'] as &$cv){
			$cv['scates']=pdo_getall('jy_cmso2o_group',array('uniacid'=>$_SESSION['uniacid'],'parentid'=>$cv['id']),array('title','id'),'id');
		}
	}
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['title']){
		exi('请填写标题','error');
	}
	if(!$postdata['uid']){
		exi('请选择管理员','error');
	}
	if(!$postdata['logo']){
		exi('请上传logo','error');
	}
	if(!$postdata['address']){
		exi('请填写地址','error');
	}
	$data=array_fields(array('status','title','type','uid','logo','bgcolor','bgimg','onlinetime','group_id','ccate','scate','url','image','ishome','lat_lng','address','price','username','phone','desc','ishot','ewm','ishot','ishow'),$postdata);
	$data['province']=$postdata['location']['province'];
	$data['district']=$postdata['location']['district'];
	$data['city']=$postdata['location']['city'];
	
	$data['onlinetime']=json_encode($data['onlinetime']);
	$data['lat_lng']=json_encode($data['lat_lng']);
	$data['hdp']=json_encode($postdata['hdp']);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_cmso2o_list',$data,array('id'=>$id));
		exi('设置成功！','','list');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_cmso2o_list',$data);
		exi('添加成功！','','list');
	}
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_cmso2o_list',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_cmso2o_list',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>